
package kaizone.haining.chaobohui.activity;

import java.io.Serializable;
import java.security.spec.MGF1ParameterSpec;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.CBHApplication;
import kaizone.haining.chaobohui.fragment.DetailCommentFragment;
import kaizone.haining.chaobohui.fragment.DetailContent;
import kaizone.haining.chaobohui.fragment.DetailNewsFragment;
import kaizone.haining.chaobohui.fragment.DetailTushuoFragment;
import kaizone.haining.chaobohui.fragment.DetailWebFragment;
import kaizone.haining.chaobohui.fragment.NearbyFragment;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.NewsItem;
import kaizone.haining.chaobohui.json.TushuoItem;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.ImageCache.ImageCacheParams;
import kaizone.haining.chaobohui.widget.CBHBotttom;
import kaizone.haining.chaobohui.widget.CBHHead;
import kaizone.haining.chaobohui.widget.UsersActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;

public class DetailActivity extends FragmentActivity implements CBHHead.OnLeftImageClickListener,
        CBHHead.OnRightClickListener {

    public static final String FLAG = "flag";

    public static final int FLAG_NEWS = 0x0001;

    public static final int FLAG_HUODONG = 0x0002;

    public static final int FLAG_TUSHUO = 0x0003;

    public static final int FLAG_OPENWEB = 0x0004;

    public static final int FLAG_NEARBY = 0x0005;

    private static final String KEY = "detail";

    private CBHApplication mCbhApp;

    private CBHHead mHead;

    private UsersActionBar mBottom;

    private Fragment mContent;

    private FragmentManager mFragmentManager;

    private ImageFetcher mImageFetcher;

    private int mRequestId;

    private String mRequestUrl;

    private String mWebOpenUrl;

    private String mRequestName;

    private int mFlag;

    private int mRightBtnState;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        Bundle bd = getIntent().getExtras();
        if (bd != null) {
            mFlag = bd.getInt(FLAG);
            mRequestId = bd.getInt(Item.ID);
            mRequestUrl = bd.getString(NewsItem.CONTENTURL);
            mWebOpenUrl = bd.getString(DetailWebFragment.WEB_FLAG);
            mRequestName = bd.getString(TushuoItem.NAME);
        }

        mCbhApp = (CBHApplication) getApplication();
        mFragmentManager = getSupportFragmentManager();
        initImageFetcher();
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.detail);
        mHead = (CBHHead) findViewById(R.id.head);
        mBottom = (UsersActionBar) findViewById(R.id.bottom);

        initHead();
        initContent();
        initButtom();
    }

    private void initHead() {
        mRightBtnState = 0;
        if (mFlag == FLAG_NEWS) {
            mHead.setText("");
            int color = getResources().getColor(android.R.color.transparent);
            mHead.setBackgroundColor(color);
            mHead.setLeftImageResource(R.drawable.btn_backblue);
            mHead.addRightButton();
        } else if (mFlag == FLAG_HUODONG) {
            String text = getString(R.string.hdxq);
            mHead.setText(text);
            mHead.setLeftImageResource(R.drawable.btn_backwhite);
            mHead.addRightButton();
        } else if (mFlag == FLAG_TUSHUO) {
            mHead.setVisibility(View.GONE);
        } else if (mFlag == FLAG_OPENWEB) {
            mHead.setText("");
            int color = getResources().getColor(android.R.color.transparent);
            mHead.setBackgroundColor(color);
            mHead.setLeftImageResource(R.drawable.btn_backblue);
        } else if (mFlag == FLAG_NEARBY) {
            mHead.setText("");
            int color = getResources().getColor(android.R.color.transparent);
            mHead.setBackgroundColor(color);
            mHead.setLeftImageResource(R.drawable.btn_backblue);
        }
        mHead.setLeftImageListener(this);
        mHead.setRightClickListener(this);
    }

    private void initContent() {
        if (mFlag == FLAG_NEWS) {
            mContent = DetailNewsFragment.newInstance(mRequestId);
        } else if (mFlag == FLAG_HUODONG) {
            // mContent = new DetailContent();
            // ((DetailContent) mContent).setRequestId(mRequestId);
            mContent = DetailContent.newInstance(mRequestId);
        } else if (mFlag == FLAG_TUSHUO) {
            mContent = DetailTushuoFragment.newInstance(mRequestId);
        } else if (mFlag == FLAG_OPENWEB) {
            mContent = DetailWebFragment.newInstance(mWebOpenUrl);
        } else if (mFlag == FLAG_NEARBY) {
            mContent = NearbyFragment.newInstance();
        }

        mFragmentManager.beginTransaction().replace(R.id.body, mContent).commit();
    }

    private void initButtom() {
        mBottom.setFlag(mFlag);
        if (mFlag == FLAG_NEWS) {
            mBottom.addShare();
        } else if (mFlag == FLAG_HUODONG) {
            mBottom.setActivityId(mRequestId);
            mBottom.addCheckins();
            mBottom.addFollows();
            mBottom.addShare();
        } else if (mFlag == FLAG_TUSHUO) {
            mBottom.addShare();
        } else if (mFlag == FLAG_OPENWEB) {
            // mBottom.addFollows();
            mBottom.addFavorite();
            mBottom.setSubjectId(mRequestId);
        } else if (mFlag == FLAG_NEARBY) {

        }
    }

    private void initImageFetcher() {
        ImageCacheParams cacheParams = new ImageCacheParams(this, CBHActivity.IMAGE_CACHE_DIR);
        cacheParams.setMemCacheSizePercent(this, 0.25f);
        mImageFetcher = new ImageFetcher(this, 200);
        mImageFetcher.setLoadingImage(null);
        mImageFetcher.addImageCache(mCbhApp, cacheParams);
    }

    @Override
    public void onLeftImageClick(View v) {
        if (mRightBtnState == 1) {
            backCommentFrament();
        } else {
            finish();
        }
    }

    @Override
    public void onRightClick(View v) {
        if (mFlag == FLAG_NEWS) {
            mContent = DetailCommentFragment.newInstance(FLAG_NEWS, mRequestId);
        } else if (mFlag == FLAG_HUODONG) {
            mContent = DetailCommentFragment.newInstance(FLAG_HUODONG, mRequestId);
        }
        mRightBtnState = 1;
        mHead.removeRightButton();
        mFragmentManager.beginTransaction().replace(R.id.body, mContent).commit();
    }

    public void backCommentFrament() {
        mRightBtnState = 0;
        mHead.addRightButton();
        if (mFlag == FLAG_NEWS) {
            mContent = DetailNewsFragment.newInstance(mRequestId);
        }//
        else if (mFlag == FLAG_HUODONG) {
            mContent = DetailContent.newInstance(mRequestId);
        }
        mFragmentManager.beginTransaction().replace(R.id.body, mContent).commit();
    }

    public ImageFetcher getImageFetcher() {
        return mImageFetcher;
    }

    public UsersActionBar getUsersActionBar() {
        return mBottom;
    }

    public static void come(Context context, Item item) {
        int id = item.getId();
        come(context, id);
    }

    public static void come(Context context, int flag, int id) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, flag);
        bd.putInt(Item.ID, id);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void come(Context context, int flag, String url) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, flag);
        bd.putString(NewsItem.CONTENTURL, url);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void come(Context context, Serializable obj) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putSerializable(DetailActivity.KEY, obj);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void openWeb(Context context, String url) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, FLAG_OPENWEB);
        bd.putString(DetailWebFragment.WEB_FLAG, url);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void openSubject(Context context, String url, int id) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, FLAG_OPENWEB);
        bd.putInt(Item.ID, id);
        bd.putString(DetailWebFragment.WEB_FLAG, url);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void openTushuo(Context context, String name) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, FLAG_TUSHUO);
        bd.putString(TushuoItem.NAME, name);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void openTushuo(Context context, int id) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, FLAG_TUSHUO);
        bd.putInt(TushuoItem.ID, id);
        intent.putExtras(bd);
        context.startActivity(intent);
    }

    public static void openNearby(Context context) {
        Intent intent = new Intent(context, DetailActivity.class);
        Bundle bd = new Bundle();
        bd.putInt(DetailActivity.FLAG, FLAG_NEARBY);
        intent.putExtras(bd);
        context.startActivity(intent);
    }
}
